package com.dhcc.DRGsHosp.application.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Named;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.JdbcTemplateObj;
import org.dayatang.querychannel.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.MiMonthProPortionApplication;
import com.dhcc.DRGsHosp.core.domain.MiMonthProPortion;
import com.dhcc.DRGsHosp.core.domain.MrMedinsFund;

@Named
@Transactional
public class MiMonthProPortionApplicationImpl implements MiMonthProPortionApplication {
	
	private QueryChannelService queryChannel;
	@Autowired  
	private JdbcTemplateObj jdbcTemplateObj; 
	
	
    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
    
    private EntityRepository repository;
	
	private EntityRepository getRepository() {
		if (repository == null) {
			repository = InstanceFactory.getInstance(
					EntityRepository.class, "repository");
		}
		return repository;
	}

	public MiMonthProPortion getMiMonthProPortion(String mimpMonth) {
		return MiMonthProPortion.get(MiMonthProPortion.class, mimpMonth);
	}
	
	public void creatMiMonthProPortion(MiMonthProPortion miMonthProPortion) {
		miMonthProPortion.save();
	}
	
	public void updateMiMonthProPortion(MiMonthProPortion miMonthProPortion) {
		miMonthProPortion .save();
	}
	
	public void removeMiMonthProPortion(MiMonthProPortion miMonthProPortion) {
		if(miMonthProPortion != null){
			miMonthProPortion.remove();
		}
	}
	
	public void removeMiMonthProPortions(Set<MiMonthProPortion> miMonthProPortions) {
		for (MiMonthProPortion miMonthProPortion : miMonthProPortions) {
			miMonthProPortion.remove();
		}
	}
	
	public List<MiMonthProPortion> findAllMiMonthProPortion() {
		return MiMonthProPortion.findAll(MiMonthProPortion.class);
	}

	/* (non-Javadoc)
	 * @see com.dhcc.DRGsHosp.application.MiMonthProPortionApplication#addHCTotalFund(java.lang.String, java.lang.String)
	 */
	@Override
	public void addHCTotalFund(String HCTotalFundYear, String HCTotalFund) {
		String selectSql = "SELECT * FROM DRGS.T_QUOTAMANAGE Q WHERE Q.PARQUOTA_CODE='HCTotalFund' AND Q.MANAGE_YEAR='"+HCTotalFundYear+"'";
		String updateSql = "UPDATE DRGS.T_QUOTAMANAGE Q SET Q.MANAGE_BENCHMARK='"+HCTotalFund+"' WHERE Q.PARQUOTA_CODE='HCTotalFund' AND Q.MANAGE_YEAR='"+HCTotalFundYear+"'";
		String insertSql = "INSERT INTO DRGS.T_QUOTAMANAGE (MANAGE_YEAR,PARQUOTA_CODE,MANAGE_BENCHMARK) VALUES ('"+HCTotalFundYear+"','HCTotalFund','"+HCTotalFund+"')";
		List<Object[]>  resultList = getQueryChannelService().createSqlQuery(selectSql).list();
		if (resultList.size()>0) {
			getRepository().createSqlQuery(updateSql).executeUpdate();
		} else {
			getRepository().createSqlQuery(insertSql).executeUpdate();
		}
	}

	/**
	 * @methodname :selectMiMonthProByType
	 * @Description : 根据基金类型查询查询基金的所有年份每个月的分配比例
	 * @author:yujiaolong
	 * @return InvokeResult
	 * @date :2017年6月22日
	 */
	//根据基金类型缓存该基金的每个月的分配比例
	public List<Map<String,Object>> selectMiMonthProByType(Map<String, Object> dto) {
		// TODO Auto-generated method stub
		StringBuilder sql = new StringBuilder();
		String fundType = dto.get("fundType") == null ? "" : dto.get("fundType").toString();
		String hospCode = dto.get("hospCode") == null ? "" : dto.get("hospCode").toString();
		sql.append(" select substr(tm.MIMP_MONTH, 0, 4) year, substr(tm.MIMP_MONTH, 5, 2) month, tm.MIMP_VALUE mimpValue from DRGs.T_MIMONTHPROPORTION tm ");
		sql.append(" where tm.FUND_TYPE = '"+fundType+"' and instr('"+ hospCode +"', tm.HOSP_CODE)>0  order by tm.MIMP_MONTH "  );
		List<Map<String,Object>>  resultList=jdbcTemplateObj.queryForList(sql.toString());
		return resultList;
	}

	
	/**
	 * @methodname :addMonthProporByType
	 * @Description : 保存和更新基金的所有年份每个月的分配比例
	 * @author:yujiaolong
	 * @return InvokeResult
	 * @date :2017年6月23日
	 */
	@Override
	public void addMonthProporByType(Map<String, Object> dto) {
		// TODO Auto-generated method stub
		String mimpMonth = dto.get("mimpMonth").toString();
		String fundType =dto.get("fundType").toString();
		Double mimpValue = new Double(dto.get("minmpValue").toString());
		String hospCode = dto.get("hospCode") == null ? "" : dto.get("hospCode").toString();
		String fundId=hospCode+"_"+fundType+"_"+mimpMonth;
		MiMonthProPortion miMonthProPortion=new MiMonthProPortion();
		miMonthProPortion.setFundId(fundId);
		miMonthProPortion.setFundType(fundType);
		miMonthProPortion.setHospCode(hospCode);
		miMonthProPortion.setMimpMonth(mimpMonth);
		miMonthProPortion.setMimpValue(mimpValue);
		String selectSql = " select t.fund_id from DRGs.T_MIMONTHPROPORTION t where t.fund_id='"+fundId+"'";
		List<Object[]>  resultList = getQueryChannelService().createSqlQuery(selectSql).list();
		if (resultList.size()>0) {
			String updateSql = "UPDATE DRGS.T_MIMONTHPROPORTION Q SET Q.MIMP_VALUE="+mimpValue+" WHERE Q.FUND_ID='"+fundId+"'";
			getRepository().createSqlQuery(updateSql).executeUpdate();
		} else {
			miMonthProPortion.save();
		}
	}
	
	/**
	 * 
	 * @methodname    :addMonthProportion
	 * @description   :保存医保总额
	 * @param         :
	 * @author        :yujiaolong
	 * @return        :
	 * @date          :2017年6月23日
	 */
	@Override
	public void addHCTotalFundByType(Map<String, Object> dto) {
		
		String HCTotalFundYear = dto.get("HCTotalFundYear").toString();
		String HCTotalFund = dto.get("HCTotalFund").toString();
		String fundType = "HCTotalFund_"+dto.get("fundType").toString();
		String hospCode = dto.get("hospCode") == null ? "" : dto.get("hospCode").toString();
		String fundId=hospCode+"_"+fundType+"_"+HCTotalFundYear;
		MrMedinsFund mrMedinsFund=new MrMedinsFund();
		mrMedinsFund.setFundId(fundId);
		mrMedinsFund.setHospCode(hospCode);
		mrMedinsFund.setMedinsFundValue(HCTotalFund);
		mrMedinsFund.setMedinsFundCode(fundType);
		mrMedinsFund.setMedinsFundYear(HCTotalFundYear);
		String selectSql = " select t.fund_id from DRGs.MR_MEDINS_FUND t where t.fund_id='"+fundId+"' and t.MEDINS_FUND_YEAR='"+HCTotalFundYear+"' ";
		List<Object[]>  resultList = getQueryChannelService().createSqlQuery(selectSql).list();
		if (resultList.size()>0) {
			String updateSql = "UPDATE DRGS.MR_MEDINS_FUND  SET MEDINS_FUND_VALUE="+HCTotalFund+" WHERE FUND_ID='"+fundId+"' and MEDINS_FUND_YEAR='"+HCTotalFundYear+"' ";
			getRepository().createSqlQuery(updateSql).executeUpdate();
		} else {
			mrMedinsFund.save();
		}
	}

	/**
	 * @methodname :getFundByType
	 * @Description : 根据年份获取基金总额
	 * @author:yujiaolong
	 * @return InvokeResult
	 * @date :2017年6月27日
	 */
	@Override
	//根据基金类型和年份缓存基金总额
	public List<Map<String,Object>> getFundByType(Map<String, Object> dto) {
		String HCTotalFundYear = dto.get("HCTotalFundYear").toString();
		String fundType = "HCTotalFund_"+dto.get("fundType").toString();
		String hospCode = dto.get("hospCode") == null ? "" : dto.get("hospCode").toString();
		String selectSql = "SELECT T.MEDINS_FUND_VALUE FROM DRGS.MR_MEDINS_FUND T WHERE T.MEDINS_FUND_CODE='"+fundType+"'  AND T.MEDINS_FUND_YEAR='"+HCTotalFundYear+"' and instr('"+ hospCode +"',T.HOSP_CODE)>0  ";
		return jdbcTemplateObj.queryForList(selectSql);
	}

}
